package com.newbingo.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IOperatorPerviewDao;
import com.newbingo.entity.OperatorPerview;

public class OperatorPerviewDao extends HibernateDaoSupport implements IOperatorPerviewDao{
	private static final Log log=LogFactory.getLog(OperatorPerviewDao.class);
	
	/*
	 * 根据操作员ID和权限ID查询是否有此操作员的权限记录
	 */
	public List findByOpIdAndPwId(Integer operatorId,Integer perviewId){
		log.debug("finding OperatorPerview instance by operatorId and PerviewId");
		try{
			Criteria cri=getSession().createCriteria(OperatorPerview.class);
			cri.createAlias("operator", "op");
			cri.createAlias("perview", "pw");
			cri.add(Expression.eq("op.uno", operatorId));
			cri.add(Expression.eq("pw.perviewId", perviewId));
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			List<OperatorPerview> oppw=cri.list();
			return oppw;
		}catch(RuntimeException re){
			log.error("find failed");
			throw re;
		}
	}
	
	/*
	 * 保存
	 */
	public void save(OperatorPerview instance){
		log.debug("save OperatorPerview instance");
		try{
			getSession().save(instance);
			log.info("save successful");
		}catch(RuntimeException re){
			log.error("save failed");
			throw re;
		}
	}
	
	/*
	 * 删除
	 */
	public void delete(OperatorPerview instance){
		log.debug("delete OperatorPerview instance");
		try{
			OperatorPerview oppw=this.findById(instance.getOpId());
			getSession().delete(oppw);
		}catch(RuntimeException re){
			log.error("delete failed");
			throw re;
		}
	}
	
	/*
	 * 根据ID查询
	 */
	public OperatorPerview findById(Integer id){
		log.debug("find OperatorPerivew instance by id");
		try{
			OperatorPerview oppw=(OperatorPerview)getSession().get(OperatorPerview.class, id);
			return oppw;
		}catch(RuntimeException re){
			log.error("find failed");
			throw re;
		}
	}
	
	/*
	 * 根据操作员的ID删除权限
	 */
	public void deleteByOperatorId(Integer opId){
		log.debug("delete OperatorPerview instances by OperatorID:"+opId);
		try{
			String hql="delete from OperatorPerview as oppw inner join oppw.operator as operator where operator.uno="+opId;
			Query query=getSession().createQuery(hql);
			query.executeUpdate();
		}catch(RuntimeException re){
			log.error("delete failed");
			throw re;
		}
		
		
	}
}
